package com.ssy.lingxi.product.controller.mobile;

import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.model.vo.PageVO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.product.api.model.response.GoodsResponse;
import com.ssy.lingxi.product.entity.Goods;
import com.ssy.lingxi.product.service.IGoodsService;
import org.modelmapper.TypeToken;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @menu: APP-货品管理
 * 货品管理
 * @author jw.chen
 * @version 2.0.0
 * @date 2020/6/30
 */
@RestController
@RequestMapping("/product/mobile/goods")
public class MobileGoodsController extends BaseController {

    @Resource
    private IGoodsService goodsService;

    /**
     * 查询货品列表
     * @param pageVO 分页实体
     * @param name 货品名称/货号/品牌名称
     * @return
     */
    @RequestMapping(value = "/getGoodsList", method = RequestMethod.GET)
    public Wrapper<PageData<GoodsResponse>> getGoodsList(PageVO pageVO, @RequestParam("name") String name) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        Page<Goods> result = goodsService.getAppGoodsList(sysUser, pageVO, name);
        List<GoodsResponse> resultList = this.modelMapper.map(result.getContent(), new TypeToken<List<GoodsResponse>>(){}.getType());
        return Wrapper.success(new PageData<>(result.getTotalElements(), resultList));
    }

    /**
     * 查询货品信息
     * @param id 货品id
     * @return Wrapper<?>
     */
    @RequestMapping(value = "/getGoods", method = RequestMethod.GET)
    public Wrapper<GoodsResponse> getGoods(@RequestParam("id") Long id) {
        UserLoginCacheDTO sysUser = this.getSysUser();
        Goods goods = goodsService.getGoods(sysUser, id);
        if(goods != null){
            return Wrapper.success(this.modelMapper.map(goods, GoodsResponse.class));
        }else{
            return Wrapper.success(null);
        }
    }
}
